<?php

namespace App\Admin\Controllers\House;

use App\Models\Common\Area;
use App\Models\Common\CommonParam;
use App\Models\House\House;
use App\Models\House\Landlord;
use App\Models\Shop\Areainfo;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class HouseController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '房源管理';
    protected $area = [];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new House());

        $selltype =  CommonParam::query()
            ->where('field', 'selltype')
            ->first();

        $grid->column('id', __('编号'));
        $grid->column('village', __('field.village'));
        $grid->column('price', __('field.price'));
        $grid->column('price_unit', __('field.price_unit'));
        $grid->column('selltype', __('field.selltype'))->select(
            CommonParam::query()->where('parent_id',$selltype->id)->get()->pluck('name','id')
        );
        $grid->column('size', __('field.size'));
        $grid->column('total_floors', __('field.total_floors'));
        $grid->column('area', __('field.area'))->display(function($val){
              $str = '';
              $textArr = array_map(function ($item){
                  $res = $item['text']??'';
                  return $res;
              },$val);
              $str = implode('/',$textArr);
              return $str;
        });
        $grid->column('detail_address', __('field.detail_address'));
        $grid->column('internal_structure', __('field.internal_structure'));
        $grid->column('landlord', __('field.landlord_id'))->display(function($item){
            return $item['name']."[".$item['phone'].']';
        });
        $grid->column('status', __('field.status'))->select(House::STATUS);
        $grid->column('user.nickname', __('field.user_id'));
        $grid->column('remark', __('field.remark'))->modalinfo();
        $grid->column('created_at', __('field.created_at'));
//        $grid->column('updated_at', __('field.updated_at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(House::findOrFail($id));

        $show->field('id', __('field.id'));
        $show->field('price', __('field.price'));
        $show->field('price_unit', __('field.price_unit'));
        $show->field('remark', __('field.remark'));
        $show->field('village', __('field.village'));
        $show->field('size', __('field.size'));
        $show->field('selltype', __('field.selltype'));
        $show->field('total_floors', __('field.total_floors'));
        $show->field('detail_address', __('field.detail_address'));
        $show->field('area', __('field.area'));
        $show->field('internal_structure', __('field.internal_structure'));
        $show->field('landlord.name', __('field.landlord_id'));
        $show->field('status', __('field.status'));
        $show->field('user_id', __('field.user_id'));
        $show->field('created_at', __('field.created_at'));
        $show->field('updated_at', __('field.updated_at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new House());

        $form->column(2/5, function ($form){
            $form->select('province','省')->options(function(){
                $data = Area::query()->where('pcode',0)->get()->pluck('text','code');
                return $data;
            })->load('city', '/dinggoapi/common/getchildformat')->rules('required');
            $form->select('city','城市')->options(function($id){

                $city = Area::query()->where('code',$id)->first();
                if (!$city) {
                    return [];
                }
                $cityes = Area::query()->where('pcode',$city->pcode)->get()->pluck('text','code');
                return $cityes;
            })->load('town', '/dinggoapi/common/getchildformat')->rules('required');
            $form->select('town','区')->options(function($id){

                $city = Area::query()->where('code',$id)->first();
                if (!$city) {
                    return [];
                }
                $cityes = Area::query()->where('pcode',$city->pcode)->get()->pluck('text','code');
                return $cityes;
            });
//        $form->hidden('area','区');
            $form->text('village', __('field.village'));

            $form->decimal('price', __('field.price'));
            $form->text('price_unit', __('field.price_unit'))->default('万');
            $form->text('remark', __('field.remark'));

            $form->decimal('size', __('field.size'))->help('m2');



            $selltype =  CommonParam::query()
                ->where('field', 'selltype')
                ->first();

            $form->select('selltype', __('field.selltype'))->options(
                CommonParam::query()->where('parent_id',$selltype->id)->get()->pluck('name','id')
            );
            $form->text('total_floors', __('field.total_floors'));



            $form->text('detail_address', __('field.detail_address'));

            $form->text('internal_structure', __('field.internal_structure'))->default('三室两厅两卫')->help('三室两厅两卫');
            $form->select('landlord_id', __('field.landlord_id'))->options(Landlord::query()->get()->pluck('name','id'));
            $form->select('status', __('field.status'))->options(House::STATUS)->default(1);
            $form->hidden('user_id', __('field.user_id'))->default(0);
        });

        $form->column(3/5, function (Form $form){
            $form->hasMany('imges',__('房源图片/视频'),function ($form){
                $form->file('file','图片/文件');//->removable()->uniqueName();

//                    ->options([
//                    "allowedFileExtensions"=> ['png','jpg','gif','mp4','ogg'],
//                    "showCaption"=> true,
//                ]);
            });
        });

        $form->saving(function(Form $form){
            $area = [];
            if ($form->province) {
                $area[] = Area::query()->where('code', $form->province)->select(['code','text'])->first()->toArray();
                $area[] = Area::query()->where('code', $form->city)->select(['code','text'])->first()->toArray();
                $area[] = Area::query()->where('code', $form->town)->select(['code','text'])->first()->toArray();
                $this->area = $area;
            }

        });
        $form->saved(function(Form $form){
            if(!empty($this->area)) {
                $form->model()->area = $this->area;
                $form->model()->save();
            }

        });
        return $form;
    }
}
